package main;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Jdbc {
	
	private static final String url = "jdbc:mysql://localhost:3306/garden3";
	private static final String className = "com.mysql.jdbc.Driver";
	private static final String userName = "root";
	private static final String passWord = "root";
	private static Connection con = null;
	private static ResultSet rs = null;
	private static PreparedStatement ps = null;
	// 静态代码块加载驱动
	static {
		try {
			Class.forName(className).newInstance();
			con = DriverManager.getConnection(url, userName, passWord);
		} catch (com.mysql.jdbc.exceptions.jdbc4.CommunicationsException e) {
			close();
			System.out.println("连接本地数据库1");
			String url1 = "jdbc:mysql://localhost:3306/student";
			String userName1 = "root";
			String passWord1 = "root";
		try {
			con = DriverManager.getConnection(url1, userName1, passWord1);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}



	/** * 数据库查询操作 */
	public static ResultSet query(String sql, Object... objs) {
		try {
			if (con == null || con.isClosed()) {
				con = DriverManager.getConnection(url, userName, passWord);
			}
			ps = con.prepareStatement(sql);
			for (int i = 0; i < objs.length; i++) {
				ps.setObject(i + 1, objs[i]);
			}
			rs = ps.executeQuery();
		} catch (com.mysql.jdbc.exceptions.jdbc4.CommunicationsException e) {
			close();
			System.out.println("连接本地数据库2");
			String URL1 = "jdbc:mysql://localhost:3306/nywlw";
			String USERNAME1 = "root";
			String PASSWORD1 = "root";
			try {
				con = DriverManager.getConnection(URL1, USERNAME1, PASSWORD1);
				ps = con.prepareStatement(sql);
				/*for (int i = 0; i < objs.length; i++) {
					ps.setObject(i + 1, objs[i]);
				}*/
				rs = ps.executeQuery();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		//	e.printStackTrace();
		} catch (SQLException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		return rs;
	}
	
	/** * 数据库增删改操作 */
	public static int update(String sql, Object... objs) {
//		if (Main.using == true) {
//			try {
//				Thread.sleep(500);
//			} catch (InterruptedException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//		Main.using = true;
		int intRet = 0;
		try {
			if (con == null || con.isClosed()) {
				con = DriverManager.getConnection(url, userName, passWord);
			}
			ps = con.prepareStatement(sql);

			for (int i = 0; i < objs.length; i++) {
				ps.setObject(i + 1, objs[i]);
			}
			intRet = ps.executeUpdate();
		} catch (com.mysql.jdbc.exceptions.jdbc4.CommunicationsException e) {
			close();
			System.out.println("连接本地数据库3");
			String URL1 = "jdbc:mysql://localhost:3306/nywlw";
			String USERNAME1 = "root";
			String PASSWORD1 = "root";
			try {
				con = DriverManager.getConnection(URL1, USERNAME1, PASSWORD1);
				ps = con.prepareStatement(sql);
				for (int i = 0; i < objs.length; i++) {
					ps.setObject(i + 1, objs[i]);
				}
				intRet = ps.executeUpdate();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} catch (NullPointerException e) {
			System.out.println(e.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		Main.using = false;
		return intRet;

	}
	
	
	
	
	/** * 关闭对象 */
	public static void close() {
		try {
			if (rs != null) {
				rs.close();
				rs = null;
			}
			if (ps != null) {
				ps.close();
				ps = null;
			}
			if (con != null) {
				con.close();
				con = null;
			}
		} catch (Exception e) {
			System.out.println("[异常] Close()");
		}
	}
}
